package sv.com.renacempleo.dao.ctg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.renacempleo.dao.BaseDaoImpl;
import sv.com.renacempleo.dto.ctg.CtgEstadoPostulanteVinculacion;
import sv.com.renacempleo.exception.WebException;

public class CtgEstadoPostulanteVinculacionDaoImpl extends BaseDaoImpl implements CtgEstadoPostulanteVinculacionDao{

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllActivesAsArray()
			throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgEstadoPostulanteVinculacion.class);
		criteria.addOrder(Order.asc("ctgEstadoPostulanteVinculacionId"));
		criteria.addOrder(Order.asc("ctgEstadoPostulanteVinculacionNombre"));
		criteria.add(Restrictions.eq("ctgEstadoPostulanteVinculacionEstado", "1"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgEstadoPostulanteVinculacionId")).
				add(Projections.property("ctgEstadoPostulanteVinculacionNombre"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllAsArray() throws WebException {
		DetachedCriteria criteria = DetachedCriteria.forClass(CtgEstadoPostulanteVinculacion.class);
		criteria.addOrder(Order.asc("ctgEstadoPostulanteVinculacionId"));
		criteria.addOrder(Order.asc("ctgEstadoPostulanteVinculacionNombre"));
		criteria.setProjection(Projections.projectionList().
				add(Projections.property("ctgEstadoPostulanteVinculacionId")).
				add(Projections.property("ctgEstadoPostulanteVinculacionNombre")).
				add(Projections.property("ctgEstadoPostulanteVinculacionDescripcion")).
				add(Projections.property("ctgEstadoPostulanteVinculacionOrden")).
				add(Projections.property("ctgEstadoPostulanteVinculacionColocacion")).
				add(Projections.property("ctgEstadoPostulanteVinculacionNoColocacion")).
				add(Projections.property("ctgEstadoPostulanteVinculacionVinculacion")).
				add(Projections.property("ctgEstadoPostulanteVinculacionEstado"))
		);
		return (List<Object[]>) findByCriteria(criteria);
	}
	
	@Override
	public void delete(Long ctgEstadoPostulanteVinculacionId)
			throws WebException {
		String hqlQuery = "delete from CtgEstadoPostulanteVinculacion where" +
				" ctgEstadoPostulanteVinculacionId = :ctgEstadoPostulanteVinculacionId";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("ctgEstadoPostulanteVinculacionId", ctgEstadoPostulanteVinculacionId);
        executeNonHibernateQuery(hqlQuery, parameters);
		
	}

	@Override
	public void toggleActive(Long ctgEstadoPostulanteVinculacionId,
			String ctgEstadoPostulanteVinculacionEstado) throws WebException {
		String hqlQuery = "update CtgEstadoPostulanteVinculacion set " +
						"ctgEstadoPostulanteVinculacionEstado = :ctgEstadoPostulanteVinculacionEstado where" +
						" ctgEstadoPostulanteVinculacionId = :ctgEstadoPostulanteVinculacionId";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("ctgEstadoPostulanteVinculacionId", ctgEstadoPostulanteVinculacionId);
		parameters.put("ctgEstadoPostulanteVinculacionEstado", "1".equals(ctgEstadoPostulanteVinculacionEstado) ? "0" : "1");
		executeNonHibernateQuery(hqlQuery, parameters);
		
	}

}
